# coding:utf-8


def convert_n_bytes(n, b):
    bits = b * 8
    return (n + 2 ** (bits - 1)) % 2 ** bits - 2 ** (bits - 1)


def convert_4_bytes(n):
    return convert_n_bytes(n, 4)


def getHashCode(s):
    h = 0
    n = len(s)
    for i, c in enumerate(s):
        h = h + ord(c) * 31 ** (n - 1 - i)
    return convert_4_bytes(h)


# 计算文件的token
def generateToken(name, size):
    if not name or not size:
        return ''
    code = getHashCode(name)
    sign = 'A' if code > 0 else 'B'
    token = sign + str(abs(code)) + '_' + size
    return token


if __name__ == '__main__':
    name = '1.png'
    size = '321597'  # A46...
    print(generateToken(name, size))

